namespace SilverSea.Untilities
{
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Windows.Forms;

    public static class ByteUntils
    {
        public static byte[] GetByteArray(Image image)
        {
            try
            {
                if (image != null)
                {
                    MemoryStream stream = new MemoryStream();
                    image.Save(stream, ImageFormat.Jpeg);
                    return stream.ToArray();
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show("An error occur while get byte array from image: " + exception.Message);
            }
            return null;
        }

        public static Image GetImage(byte[] bytearray)
        {
            try
            {
                if (bytearray != null)
                {
                    MemoryStream stream = new MemoryStream();
                    stream.Write(bytearray, 0, bytearray.Length);
                    return Image.FromStream(stream);
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show("An error occur while get image from byte array: " + exception.Message);
            }
            return null;
        }
    }
}

